package com.znyj.servlet.task;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by gaoliwei02 on 2017/12/10.
 */
@Component
public class LockTaskMapping {
    private static final Logger logger = Logger.getLogger(LockTaskMapping.class);

    private final Map<String, Integer> lockMap = new ConcurrentHashMap<>();

    public boolean isContainDevice(String deviceId) {
        if (!lockMap.containsKey(deviceId)) {
            lockMap.put(deviceId, 3);
            return false;
        }else{
            return true;
        }
    }

    //@Scheduled(cron = "0/2 * * * * ?")
    @Scheduled(fixedDelay = 10000)
    public void sendTasks() {
        //把所有有任务设备的任务发送出去
        for (Map.Entry<String, Integer> entry : lockMap.entrySet()) {
            if (entry.getValue() <= 0) {
                lockMap.remove(entry.getKey());
            } else {
                lockMap.put(entry.getKey(), entry.getValue() - 1);
            }
        }
    }
}
